/*
 * @lc app=leetcode.cn id=200 lang=golang
 *
 * [200] 岛屿数量
 */

// @lc code=start
// package test

func numIslands(grid [][]byte) int {
	m, n := len(grid), len(grid[0])
	route := make([][]bool, m)
	for i, _ := range route {
		route[i] = make([]bool, n)
	}
	count := 0
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if grid[i][j] == '1' && route[i][j] == false {
				count++
				dfs(i, j, m, n, &grid, &route)
			}
		}
	}
	return count
}

func dfs(i, j, m, n int, grid *[][]byte, route *[][]bool) {
	if i < 0 || i >= m || j < 0 || j >= n {
		return
	}
	if (*grid)[i][j] == '1' && (*route)[i][j] == false {
		(*route)[i][j] = true
		dfs(i-1, j, m, n, grid, route)
		dfs(i+1, j, m, n, grid, route)
		dfs(i, j-1, m, n, grid, route)
		dfs(i, j+1, m, n, grid, route)
	}
	return
}

// @lc code=end
